package com.example.work06;
import java.io.File;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.SearchManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity {
    Intent intent = new Intent();

    private Button button01;
    private Button button02;
    private Button button03;
    private Button button04;
    private Button button05;
    private Button button06;
    private Button button07;
    private Button button08;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //打开指定网页
        button01 = findViewById(R.id.btn_open_web_url);
        //打开拨号面板
        button02 = findViewById(R.id.btn_open_ring_pannel);

        //直接拨打指定号码
        button03 = findViewById(R.id.btn_ring_designed);
        //打开发短信的界面
        button04 = findViewById(R.id.btn_open_msg_pannel);

        //给指定的人发短信
        button05 = findViewById(R.id.btn_send_msg_designed);
        //播放指定路径音乐
        button06 = findViewById(R.id.btn_play_music);

        //卸载程序
        button07 = findViewById(R.id.btn_uninstall);
        //安装程序
        button08 = findViewById(R.id.btn_install);
        setListeners();
    }
    void setListeners(){
        OnClick onClick = new OnClick();
        button01.setOnClickListener(onClick);
        button02.setOnClickListener(onClick);
        button03.setOnClickListener(onClick);
        button04.setOnClickListener(onClick);
        button05.setOnClickListener(onClick);
        button06.setOnClickListener(onClick);
        button07.setOnClickListener(onClick);
        button08.setOnClickListener(onClick);
    }

    class OnClick implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                // 打开指定网页
                case R.id.btn_open_web_url:
                    // 记得加权限<uses-permission
                    //android:name="android.permission.INTERNET"/>
                    Uri uri = Uri.parse("http://www.baidu.com");
                    intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                    break;
                //打开拨号面板
                case R.id.btn_open_ring_pannel:
                    // Intent.ACTION_DIAL 跳到拨号界面
                    intent.setAction(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:12345678910"));
                    startActivity(intent);

                    break;
                //直接拨打指定号码
                case R.id.btn_ring_designed:
                    // 记得加权限<uses-permission
                    // android:name="android.permission.CALL_PHONE"/>
                    // Intent.ACTION_CALL 直接拨打指定号码
                    intent.setAction(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:12345678910"));
                    startActivity(intent);

                    break;
                //打开发送短信的界面
                case R.id.btn_open_msg_pannel:
                    // 打开短信界面 记得加上<uses-permission
                    // android:name="android.permission.READ_SMS"/>
                    startActivity(intent);
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.setType("vnd.android-dir/mms-sms");
                    startActivity(intent);
                    break;

                //给指定的人发短信
                case R.id.btn_send_msg_designed:
                    // 发送短信 记得加上<uses-permission
                    // android:name="android.permission.SEND_SMS"/>
                    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("smsto:12345678910"));
                    intent.putExtra("sms_body", "给指定的人发短信");
                    startActivity(intent);
                    break;
                // 播放音乐
                case R.id.btn_play_music:
                    intent = new Intent(Intent.ACTION_VIEW);
                    uri = Uri.parse("file:///sdcard/doudizhu.mp3");
                    intent.setDataAndType(uri, "audio/*");
                    startActivity(intent);
                    break;
                // 卸载程序
                case R.id.btn_uninstall:
                    intent = new Intent(Intent.ACTION_DELETE);
                    uri = Uri.parse("package:com.example.exp05");
                    intent.setData(uri);
                    startActivity(intent);
                    break;
                // 安装程序
                case R.id.btn_install:
                    // /使用 ACTION_VIEW，指定 apk 包的路径安装，路径格式： /文件路径/程序名.apk
                    // 或者使用 ACTION_INSTALL_PACKAGE，通过包名匹配安装，
                    // uri = Uri.fromParts("package", strPackageName, null);
                    // intent = new Intent(Intent.ACTION_PACKAGE_ADDED, uri);
//                    uri = Uri.fromParts("package", "com.example.myapplication3", null);
//                    intent = new Intent(Intent.ACTION_INSTALL_PACKAGE, uri);
//                    startActivity(intent);
                    intent= new Intent(Intent.ACTION_VIEW);
                    uri=Uri.fromFile(new File("/storage/sdcard/Download/keyoukongjian.apk"));
                    intent.setDataAndType(uri,"application/vnd.android.package-archive");
                    startActivity(intent);
                    break;
                default:
                    break;
            }
        }
    }
}
